package key.w8.boot.config;

import com.k8.common.factroy.BeanFactory;
import com.k8.common.util.StrUtil;
import com.k8.registry.RegistryManager;
import key.w8.boot.condition.OnK8ReferenceEnabledCondition;
import key.w8.boot.properties.ConsumerAutoConfigurationProperties;
import key.w8.boot.reference.ReferenceBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

import static com.k8.common.constants.SpiConstants.PROXY_TYPE_JAVASSIST;

/**
 * @Author: k8
 * @CreateTime: 2025-08-17
 * @Version: 1.0
 */
@Configuration
@Conditional(OnK8ReferenceEnabledCondition.class)
@EnableConfigurationProperties(ConsumerAutoConfigurationProperties.class)
public class K8ReferenceAutoConfiguration {
    @Bean
    public ReferenceBuilder referenceBuilder(){
        return new ReferenceBuilder();
    }
    @Bean
    public BeanFactory consumerBeanFactory(ConsumerAutoConfigurationProperties consumerAutoConfigurationProperties){
        String proxy = consumerAutoConfigurationProperties.getProxy();
        if (StrUtil.isEmpty(proxy)){
            proxy = PROXY_TYPE_JAVASSIST;
        }
        BeanFactory beanFactory = new BeanFactory(proxy);
        beanFactory.getOrRegistryBean(RegistryManager.class);
        return beanFactory;
    }
}
